﻿using amuudp;
using amuudp.package;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace testReceiver
{
    class Program
    {
        static void Main(string[] args)
        {
            IServerEvents serverEvents = new ServerEvents();
            UdpServer receiver = new UdpServer(1, 11000, serverEvents);
            receiver.Start();
        }
    }


    class ServerEvents : IServerEvents
    {
        public void OnReceive(Fragment fragment, IPAddress ip, int port, params Object[] args)
        {
            Console.WriteLine($"{fragment.PackageId},{fragment.Size},{ fragment.Index}");
        }

        public void OnPackageComplete(Package package, IPAddress ip, int port, params Object[] args)
        {
            UdpServer server = (UdpServer)args[0];
            Console.WriteLine(123);

            if (package.Type == PackageType.Message)
            {
                MessagePackage messagePackage = new MessagePackage(package);
                Console.WriteLine(messagePackage.Message);
                Thread.Sleep(500);
                //server.Send("connected", new IPEndPoint(ip, port));
            }
        }
    }
    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        UdpClient client = null;
    //        string receiveString = null;
    //        byte[] receiveData = null;
    //        //实例化一个远程端点，IP和端口可以随意指定，等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点  
    //        IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);

    //        while (true)
    //        {
    //            client = new UdpClient(11000);
    //            receiveData = client.Receive(ref remotePoint);//接收数据  
    //            receiveString = Encoding.Default.GetString(receiveData);
    //            Console.WriteLine(receiveString);
    //            client.Close();//关闭连接  
    //        }
    //    }
    //}
}
